package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ExhibitionCompanyDTO;
import com.eastfair.venueservice.dto.ExhibitionCompanyPageQuery;
import com.eastfair.venueservice.dto.ExhibitionCompanySaveDTO;
import com.eastfair.venueservice.dto.ExhibitionCompanyUpdateDTO;
import com.eastfair.venueservice.entity.ExhibitionCompany;
import com.eastfair.venueservice.service.ExhibitionCompanyService;
import com.eastfair.venueservice.vo.ExhibitionCompanyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 展会相关企业
 * </p>
 *
 * @author dq
 * @date 2022-09-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/exhibitionCompany")
@Api(value = "ExhibitionCompany", tags = "展会相关企业")
@PreAuth(replace = "common:exhibitionCompany:")
public class ExhibitionCompanyController extends SuperController<ExhibitionCompanyService, Long, ExhibitionCompany, ExhibitionCompanyPageQuery, ExhibitionCompanySaveDTO, ExhibitionCompanyUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ExhibitionCompanyService exhibitionCompanyService;

    @Override
    public void handlerResult(IPage<ExhibitionCompany> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ExhibitionCompany> exhibitionCompanyList = list
                .stream()
                .map((map) -> ExhibitionCompany.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(exhibitionCompanyList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ExhibitionCompanyPageQuery> params) {
        ExhibitionCompanyPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询展会企业VO.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询展会企业VO")
    @PostMapping("/query/exhibitionCompany")
    public R<Page<ExhibitionCompanyVO>> queryExhibitionCompany(@RequestBody PageParams<ExhibitionCompanyDTO> pageParams) {
        log.info("queryExhibitionCompany - 分页查询展会企业VO, pageParams={}", pageParams);
        try {
            return R.successDef(exhibitionCompanyService.listExhibitionCompanyVOs(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * ID获取展会企业详情.
     *
     * @param id the id
     * @return the by id
     */
    @ApiOperation("ID获取展会企业详情")
    @GetMapping("/get/{id}")
    public R<ExhibitionCompanyVO> getById(@PathVariable("id") Long id) {
        log.info("getById - ID获取展会企业详情, id={}", id);
        try {
            return R.successDef(exhibitionCompanyService.getExhibitionCompanyVOById(id));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 服务商ID查询展会企业VO.
     *
     * @param serviceProviderId the service provider id
     * @param exhibitionMangeId the exhibition mange id
     * @return the by service provider id
     */
    @ApiOperation("服务商ID查询展会企业VO")
    @GetMapping("/serviceProvider/get")
    public R<ExhibitionCompanyVO> getByServiceProviderId(@RequestParam("serviceProviderId") Long serviceProviderId,
                                                         @RequestParam("exhibitionMangeId") Long exhibitionMangeId) {
        log.info("getByServiceProviderId - 服务商ID查询展会企业VO, serviceProviderId={}, exhibitionMangeId={}", serviceProviderId, exhibitionMangeId);
        try {
            return R.successDef(exhibitionCompanyService.getVOByServiceProviderId(serviceProviderId, exhibitionMangeId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 保存展会企业.
     *
     * @param exhibitionCompanyDTO the exhibition company dto
     * @return the r
     */
    @ApiOperation("保存展会企业")
    @SysLog("添加展会企业")
    @PostMapping("/save")
    public R<Boolean> saveExhibitionCompany(@RequestBody ExhibitionCompanyDTO exhibitionCompanyDTO) {
        log.info("saveExhibitionCompany - 保存展会企业, exhibitionCompanyDTO={}", exhibitionCompanyDTO);
        try {
            return R.successDef(exhibitionCompanyService.saveExhibitionCompany(exhibitionCompanyDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 批量保存展会企业.
     *
     * @param list the list
     * @return the r
     */
    @ApiOperation("批量保存展会企业")
    @SysLog("添加展会企业")
    @PostMapping("/save/batch")
    public R<Boolean> saveBatch(@RequestBody List<ExhibitionCompanyDTO> list) {
        log.info("saveBatch - 批量保存展会企业, list={}", list);
        try {
            return R.successDef(exhibitionCompanyService.saveBatchExhibitionCompany(list));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 修改展会企业.
     *
     * @param exhibitionCompanyDTO the exhibition company dto
     * @return the r
     */
    @ApiOperation("修改展会企业")
    @SysLog("修改展会企业")
    @PostMapping("/update")
    public R<Boolean> updateExhibitionCompany(@RequestBody ExhibitionCompanyDTO exhibitionCompanyDTO) {
        log.info("updateExhibitionCompany - 修改展会企业, exhibitionCompanyDTO={}", exhibitionCompanyDTO);
        try {
            return R.successDef(exhibitionCompanyService.updateExhibitionCompany(exhibitionCompanyDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * id移除展会企业.
     *
     * @param id the id
     * @return the r
     */
    @ApiOperation("id移除展会企业")
    @SysLog("删除展会企业")
    @GetMapping("/remove/{id}")
    public R<Boolean> removeById(@PathVariable("id") Long id) {
        log.info("removeById - id移除展会企业, id={}", id);
        try {
            return R.successDef(exhibitionCompanyService.removeExhibitionCompanyById(id));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 服务商ID，展会ID删除展会企业.
     *
     * @return the r
     */
    @ApiOperation("服务商ID，展会ID删除展会企业")
    @SysLog("删除展会企业")
    @PostMapping("/remove/serviceProviderId")
    public R<Boolean> removeByServiceProviderId(@RequestBody ExhibitionCompanyDTO exhibitionCompanyDTO) {
        log.info("removeByServiceProviderId - 服务商ID，展会ID移除展会企业, serviceProviderId={}, exhibitionManageId={}",
                exhibitionCompanyDTO.getServiceProviderIdList(), exhibitionCompanyDTO.getExhibitionManageId());
        try {
            List<Long> serviceProviderIdList = exhibitionCompanyDTO.getServiceProviderIdList();
            Long exhibitionManageId = exhibitionCompanyDTO.getExhibitionManageId();
            return R.successDef(exhibitionCompanyService.removeByServiceProviderId(serviceProviderIdList, exhibitionManageId));
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
